//package com.xtoneict.auth.filter;
//
//
//import com.xtoneict.util.jwt.JwtHelper;
//import com.xtoneict.util.result.Result;
//import com.xtoneict.util.result.ResultCodeEnum;
//import org.springframework.util.StringUtils;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//@WebFilter(urlPatterns = "/*")
//public class loginFilter implements Filter {
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest req= (HttpServletRequest) servletRequest;
//        HttpServletResponse resp= (HttpServletResponse) servletResponse;
//        //1获取请求url。
//        String url=req.getRequestURI().toString();
//        //2判断请求url中是否包含login，如果包含，说明是登录操作，放行
//        if(url.contains("login")){
//            filterChain.doFilter(servletRequest,servletResponse);
//            return;
//        }
//        if(url.contains("getVcode")){
//            filterChain.doFilter(servletRequest,servletResponse);
//            return;
//        }
//
//        //3获取请求头中的令牌(token)
//        String token = req.getHeader("token");
//        //4判断令牌是否存在，如果不存在，返回错误结果(未登录)
//        if(!StringUtils.hasLength(token)){
////            Result error = Result.error("NOT_LOGIN");
////            String notLogin = JSONObject.toJSONString(error);
////            resp.getWriter().write(String.valueOf(ResultCodeEnum.LOGIN_AUTH));
//            resp.setStatus(401);
//            return;
//
//        }
//        //5解析token，如果解析失败，返回错误结果(未登录)
//        try {
//            JwtHelper.parseToken(token);
//        } catch (Exception e) {
//            e.printStackTrace();
////            Result error = Result.error("NOT_LOGIN");
////            String notLogin = JSONObject.toJSONString(error);
////            resp.getWriter().write(String.valueOf(ResultCodeEnum.LOGIN_ERROR));
//            resp.setStatus(401);
//            return;
//        }
//        //6放行
//        filterChain.doFilter(servletRequest,servletResponse);
//    }
//}
package com.xtoneict.auth.filter;


import com.xtoneict.util.jwt.JwtHelper;
import com.xtoneict.util.result.Result;
import com.xtoneict.util.result.ResultCodeEnum;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(urlPatterns = "/*")
public class loginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpServletResponse resp= (HttpServletResponse) servletResponse;
        //1获取请求url。
        String url=req.getRequestURI().toString();
        //2判断请求url中是否包含login，如果包含，说明是登录操作，放行
        if(url.contains("login")){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        if(url.contains("getVcode")){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }

        if("OPTIONS".equals(req.getMethod().toUpperCase())) {
            System.out.println("Method:OPTIONS");
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        //3获取请求头中的令牌(token)
        String token = req.getHeader("Token");
        //4判断令牌是否存在，如果不存在，返回错误结果
        if(!StringUtils.hasLength(token)){
            resp.setStatus(401);
            return;

        }
        //5解析token，如果解析失败，返回错误结果
        try {
            JwtHelper.parseToken(token);
        } catch (Exception e) {
            e.printStackTrace();
            resp.setStatus(401);
            return;
        }
        //6放行
        filterChain.doFilter(servletRequest,servletResponse);
    }
}